package com.demo.util;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import java.sql.PreparedStatement;


public class Conn {

	private Connection conn = null;
	Statement statement = null;
	PreparedStatement ps = null;
	
	public Conn() {
		try { 
			Properties p=new Properties();
			String prop = Conn.class.getResource("") + "dbinfo.property";
			p.load(new FileInputStream(prop.replace("file:", "")));
			
			String host = p.getProperty("host");
			String user = p.getProperty("user");
			String pwd = p.getProperty("password");
			
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(host, user, pwd);
			statement = conn.createStatement();

		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	
	public boolean update(String sql) {
		try {
			statement.executeUpdate(sql);
			
			return true;
		} catch(Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}
	
	public boolean execute(String sql) {
		try {
			ps= (PreparedStatement) conn.prepareStatement(sql);
			ps.execute();
			
			return true;
		} catch(Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}
	
	public ResultSet find(String sql) {
		ResultSet rs = null;
		
		try {
			rs = statement.executeQuery(sql);
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		
		return rs;
	}
	
}
